package com.example.kalaok;

import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class SongListAdapter extends BaseAdapter {
	private Activity mContext;
	private LayoutInflater mInflater;
	
	public SongListAdapter(Context context){
		mContext = (Activity) context;
		this.mInflater = LayoutInflater.from(context);
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return MyApplication.gSongList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null) {		
			holder=new ViewHolder();  		
			convertView = mInflater.inflate(R.layout.lvitem, null);
			holder.name = (TextView)convertView.findViewById(R.id.name);
			holder.artist = (TextView)convertView.findViewById(R.id.artist);
			holder.select = (Button)convertView.findViewById(R.id.btn_select);
			holder.cut = (Button)convertView.findViewById(R.id.btn_cut);
			convertView.setTag(holder);	
		}else {	
			holder = (ViewHolder)convertView.getTag();
		}
		final int pos = position;
		holder.name.setText((String)MyApplication.gSongList.get(position).get("name"));
		holder.artist.setText((String)MyApplication.gSongList.get(position).get("artist"));	
		holder.select.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				MyApplication.addSong(MyApplication.gSongList.get(pos));			
			}
		});
		holder.cut.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				MyApplication.insertSong(MyApplication.gSongList.get(pos));
				PlayView playview = (PlayView)mContext.findViewById(R.id.surface);
				playview.cutSong();		
			}
		});
		return convertView;
	}
	
	public final class ViewHolder{
		public TextView name;
		public TextView artist;
		public Button select;
		public Button cut;
	}
}
